package com.sz.jinzheng.controller;

import com.sz.jinzheng.client.UserCilent;
import com.sz.jinzheng.model.*;
import com.sz.jinzheng.service.RoleUserService;
import com.sz.jinzheng.service.UserInfoService;
import com.sz.jinzheng.until.UserUntil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RoleUserService roleUserService;
    @Autowired
    private UserUntil userUntil;

    @Autowired
    private UserCilent userCilent;

    /**
     * 获得员工的信息不包括用户
     *
     * @return
     */
    @RequestMapping("/getUserAll")
    public Result getUserAll(int index, int size) {
        PageBean<User> userAll = userInfoService.getUserAll(index, size);
        log.info(userAll.toString());
        return new Result(200, "成功", userAll);
    }

    /**
     * 通过uid 查询出对应的角色
     *
     * @param uid
     * @return
     */
    @RequestMapping("/getUserRoleAll")
    public Result getUserRoleAll(int uid) {
        List<Role> roleAll = userInfoService.getUserRoleAll(uid);
        return new Result(200, "成功", roleAll);
    }

    /**
     * 更新用户角色
     *
     * @param roleUsers
     * @return
     */
    @RequestMapping("/updateUserRoles")
    public Result updateUserRoles(@RequestBody RoleUsers roleUsers) {
        //首先进来的时候判断用户是否拥有角色
        RoleUsers roleUsers1 = new RoleUsers();
        roleUsers1.setUid(roleUsers.getUid());
        RoleUsers role = roleUserService.getRoleUsers(roleUsers1);
        int i = 0;
        if (role != null) {
            //如果存在那么就执行删除再添加
            i = roleUserService.deleteRoleUsers(role.getId());
            i = roleUserService.addRoleUsers(roleUsers);
        }

        if (i > 0) {
            return new Result(200, "更新成功");
        }
        return new Result(200, "更新失败");
    }

    /**
     * 添加用户
     *
     * @return
     */
    @Transactional
    @RequestMapping("/addUser")
    public Result addUser(@RequestBody User user) {
        log.info("user.toString()=" + user.toString());
        //首先添加用户 添加成功后返回主键uid
        String salt = userUntil.getSalt();
        user.setSalt(salt);
        user.setPassword(userUntil.getPwd(user.getPassword(), salt));
        int i = userInfoService.addUser(user);
        if (i > 0) {
            //如果添加成功则添加权限
            i = roleUserService.addRoleUsers(new RoleUsers(user.getRid(), user.getUid()));
        }
        if (i > 0) {
            return new Result(200, "添加成功");
        }
        return new Result(500, "添加失败");
    }

    /**
     * 更新用户是否可用
     *
     * @return
     */
    @RequestMapping("/updateUserIs")
    public Result updateUserIs(@RequestBody User user) {
        User user1 = userInfoService.getUserByKey(user.getUid());
        //如果是可用变为不可用 反之也是
        if (user1.getIs_deleted() == 0) {
            user.setIs_deleted(1);
        } else {
            user.setIs_deleted(0);
        }
        int i = userInfoService.updateUser(user);
        if (i > 0) {
            return new Result(200, "修改成功");
        }
        return new Result(500, "修改失败");
    }

    @RequestMapping("/login")
    public Result adminLogin(@RequestBody User users) {
        Result result = userCilent.login(users);
        //如果登录成功就直接判断用户是否拥有admin权限
        if (result.getCode() == 200) {
                return result;
        }
        return new Result(500, "登录失败");
    }
}
